<template>
    <div class="word square f120 white-bg ca rel">
        <div class="canvas absfull"></div>
        <canvas ref="canvas" class="full rel" style="z-index: 1"></canvas>
    </div>
</template>
<script>
    import SignaturePad from 'signature_pad';


    export default {
        props: {
            word: String
        },
        mounted() {
            const signaturePad = this.signaturePad = new SignaturePad(this.$refs.canvas);
            signaturePad.minWidth = 5;
            signaturePad.maxWidth = 10;
            signaturePad.penColor = "#383838";
            signaturePad.onEnd = () => this.$emit('toDataURL', signaturePad.toDataURL());
            this.resizeCanvas();
        },
        methods: {
            resizeCanvas() {
                const canvas = this.$refs.canvas;
                var ratio = Math.max(window.devicePixelRatio || 1, 1);
                const {offsetWidth, offsetHeight} = this.$el;
                canvas.width = offsetWidth * ratio;
                canvas.height = offsetHeight * ratio;
                canvas.getContext("2d").scale(ratio, ratio);
                this.signaturePad.clear(); // otherwise isEmpty() might return incorrect value
            },
            onBack() {
                const data = this.signaturePad.toData();
                if (data) {
                    data.pop(); // remove the last dot or line
                    this.signaturePad.fromData(data);
                }
            },
            onReset() {
                this.signaturePad.clear();
                this.$emit('toDataURL', '');
            }
        }
    };
</script>
<style lang="scss" scoped>
    .canvas {
        border: 1px solid #dcdcdc;
        border-radius: 5px;

        &:before, &:after {
            position: absolute;
            top: 50%;
            left: 50%;
            display: block;
            content: '';
            z-index: 0;
        }

        &:before {
            border-top: 1px solid #dcdcdc;
            width: 80%;
            margin-top: -0.5px;
            margin-left: -40%;
        }

        &:after {
            border-left: 1px solid #dcdcdc;
            height: 80%;
            margin-top: -40%;
            margin-left: -0.5px;
        }
    }
</style>

